
import requests
import re

# headers = {
#     # User-Agent 用户代理, 表示浏览器基本身份信息
#     'User-Agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36'
# }
# # 请求链接https://www.douyin.com/user/MS4wLjABAAAAB0-gppwu15DtJJZmMpgUqakr7Jw_pmr7skR3IW6MwCQ?modal_id=7270865943398518050
#         #https://www.douyin.com/user/MS4wLjABAAAAB0-gppwu15DtJJZmMpgUqakr7Jw_pmr7skR3IW6MwCQ?modal_id=7270865943398518050
# # url = 'https://www.douyin.com/user/MS4wLjABAAAArgJe6h-DzQcvyZ1O71yXSdJFn19Tqq8lFCIffgy5SlhwYlkseK5aM5ETF8KoaGDK?modal_id=7270476649714421046&vid=7269532986553552140'
# url = "https://www.douyin.com/user/MS4wLjABAAAAB0-gppwu15DtJJZmMpgUqakr7Jw_pmr7skR3IW6MwCQ?modal_id=7270865943398518050"
# # 发送请求
# response = requests.get(url=url, headers=headers)

# # 获取响应文本数据  获取网页源代码内容
# html_data = response.text
# print(html_data)
# # 提取标题
# title = re.findall('lark:url:video_title" content="(.*?)"/>', html_data)[0]
# print("title==={title}")
# # 提取视频信息 <经过了编码>
# video_info = re.findall('<script id="RENDER_DATA" type="application/json">(.*?)</script>', html_data)[0]
# # 解码
# info = requests.utils.unquote(video_info)
# # 把完整json数据格式字符串, 转成字典数据类型
# json_data = json.loads(info)
# # 根据冒号左边的内容[键], 提取冒号右边的内容[值]
# video_url = 'https:' + json_data['app']['videoDetail']['video']['bitRateList'][0]['playAddr'][0]['src']


# video_content = requests.get(url=video_url, headers=headers).content
# with open('video\\' + title + '.mp4', mode='wb') as f:
#     f.write(video_content)
# print(title)
# print(video_url)



# 自动化测试模块
from selenium import webdriver
# 超级鹰demo
from chaojiying import Chaojiying_Client
# 超级鹰 账号密码
from password import account, password
# 动作链
from selenium.webdriver.common.action_chains import ActionChains



# 打开浏览器, 访问网站
driver = webdriver.Chrome()
driver.get('https://www.douyin.com/user/MS4wLjABAAAAB0-gppwu15DtJJZmMpgUqakr7Jw_pmr7skR3IW6MwCQ')
# 延时
driver.implicitly_wait(10)
time.sleep(2)
# 获取验证码图片
img_label = driver.find_element_by_css_selector('.captcha_verify_container')
# 截图 保存验证码图片
img_label.screenshot('yzm.png')


# 调用打码平台 --> 帮助我们识别文字坐标
chaojiying = Chaojiying_Client(账号, 密码, '96001')
# 读取图片
im = open('yzm.png', 'rb').read()
result = chaojiying.PostPic(im, '9004')['pic_str']
for res in result.split('|'):
    x = res.split(',')[0]
    y = res.split(',')[-1]
    ActionChains(driver).move_to_element_with_offset(img_label, int(x), int(y)).click().perform()

driver.find_element_by_css_selector('.captcha_verify_action div:last-of-type').click()
time.sleep(2)driver.implicitly_wait(10)
lis = driver.find_elements_by_class_name('Eie04v01')
for li in lis:
    video_id = li.find_element_by_css_selector('a').get_attribute('href').split('/')[-1]